package com.example.a22310.myapplication.ViewModel.User;


import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Adapter.Drag.RecycleItemTouchHelper;
import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.Post;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Model.UserModel;
import com.example.a22310.myapplication.R;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class PostViewModel implements PostListener<Post> {
    private ActivityUserPostBinding binding;
    private AppCompatActivity activity;
    private int userid;
    private int page = 1;
    private boolean flag =false;



    public PostViewModel(AppCompatActivity activity, ActivityUserPostBinding binding,int userid){
        this.activity = activity;
        this.binding = binding;
        this.userid = userid;
        binding.setModel(this);
        setModel(1);
        initializa();
    }
    private void initializa(){
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                binding.refreshLayout.finishRefresh(2000);
                flag = true;
                setModel(page++);
            }
        });
        binding.refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                binding.refreshLayout.finishLoadmore(2000);
                flag = true;
                setModel(page++);

            }
        });
    }


    public void back(View view ){
        activity.onBackPressed();
    }
    @Override
    public void onSuccess(Post post) {
        if(post.getCode()!=404) {
            if(flag == true){
                List<Post.PostlistBean> postlistBeen = post.getPostlist();
                binding.recyclerViewPost.setLayoutManager(new LinearLayoutManager(activity));
                MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_post,
                        postlistBeen, BR.post);
                adapter.addAll(postlistBeen);
                binding.recyclerViewPost.setAdapter(adapter);
                ItemTouchHelper.Callback callback=new RecycleItemTouchHelper(adapter);
                ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
                itemTouchHelper.attachToRecyclerView(binding.recyclerViewPost);
                flag = false;
            }else{
                List<Post.PostlistBean> postlistBeen = post.getPostlist();
                binding.recyclerViewPost.setLayoutManager(new LinearLayoutManager(activity));
                MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_post,
                        postlistBeen, BR.post);
                binding.recyclerViewPost.setAdapter(adapter);
                ItemTouchHelper.Callback callback=new RecycleItemTouchHelper(adapter);
                ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
                itemTouchHelper.attachToRecyclerView(binding.recyclerViewPost);
            }

        }
    }

    @Override
    public void onFailure() {
        Toast.makeText(activity,"连接服务器失败",Toast.LENGTH_LONG).show();
    }

    private void setModel(int page){
        UserModel userModel=new UserModel();
        userModel.postList(PostViewModel.this,userid,page);
    }


}